//
//  SNKCollectViewController.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/16.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKCollectViewController.h"
#import "SNKDataBase.h"
#import "CollectModel.h"
#import "SNKNewsCollectCell.h"
#import "PhotoCollectModel.h"
#import "SNKPhotoCollectCell.h"
#import "SNKDataModel.h"
#import "SNKDetailWebController.h"
#import "SNKPhotoShowViewController.h"
#import "SNKVideoCollectModel.h"
#import <UIImageView+WebCache.h>
#import "SNKVideoCollectCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "SNKVideoDetailController.h"

@interface SNKCollectViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *tableview;
    int column; // 记录选中seg的序号
}

@property (nonatomic , strong) NSMutableArray *totalArr;

@end

@implementation SNKCollectViewController

/* cell的重用标识 */
static NSString * const SNKNewsCollectCellId = @"news";
static NSString * const SNKVideoCollectCellId = @"video";
static NSString * const SNKPhotoCollectCellId = @"photo";

- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if (_totalArr) {
        _totalArr = nil;
    }
    NSString *segment = [[NSUserDefaults standardUserDefaults]objectForKey:@"Segment"];
    if ([segment isEqualToString:@"2"]) {
        self.totalArr = [SNKDataBase videosDisplay];
    }else if ([segment isEqualToString:@"1"]){
        self.totalArr = [SNKDataBase photosDisplay];
    }else{
        self.totalArr = [SNKDataBase newsDisplay];
    }
    [tableview reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackgroundColor;
    
    // 设置tableView
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.tableFooterView = [[UIView alloc] init];
    
    // 设置segment
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"新闻",@"图片",@"视频",nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = [UIColor whiteColor];
    [segmentedControl addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;
    [self segmentClick:segmentedControl];
    
    [tableview registerNib:[UINib nibWithNibName:NSStringFromClass([SNKNewsCollectCell class]) bundle:nil] forCellReuseIdentifier:SNKNewsCollectCellId];
    [tableview registerNib:[UINib nibWithNibName:NSStringFromClass([SNKPhotoCollectCell class]) bundle:nil] forCellReuseIdentifier:SNKPhotoCollectCellId];
    [tableview registerNib:[UINib nibWithNibName:NSStringFromClass([SNKVideoCollectCell class]) bundle:nil] forCellReuseIdentifier:SNKVideoCollectCellId];
}

- (void)segmentClick:(UISegmentedControl *)seg
{
    if ([seg selectedSegmentIndex] == 0) {
        column = 0;
        
        self.totalArr = nil;
        self.totalArr = [SNKDataBase newsDisplay];
        [tableview reloadData];
        
    }else if ([seg selectedSegmentIndex] == 1) {
        column = 1;
        
        self.totalArr = nil;
        self.totalArr = [SNKDataBase photosDisplay];
        [tableview reloadData];
    }else {
        column = 2;
        
        self.totalArr = nil;
        self.totalArr = [SNKDataBase videosDisplay];
        [tableview reloadData];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.totalArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (column == 0) {
        SNKNewsCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:SNKNewsCollectCellId forIndexPath:indexPath];
        cell.collectModel = self.totalArr[indexPath.row];
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d", column] forKey:@"Segment"];
        return cell;
    } else if (column == 1) {
        SNKPhotoCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:SNKPhotoCollectCellId forIndexPath:indexPath];
        cell.photoCollectModel = self.totalArr[indexPath.row];
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",column] forKey:@"Segment"];
        return cell;
    } else {
        SNKVideoCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:SNKVideoCollectCellId forIndexPath:indexPath];
        
        cell.videoCollectModel = self.totalArr[indexPath.row];
        
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%d",column] forKey:@"Segment"];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (column == 0) {
        SNKDetailWebController *detailVC = [[SNKDetailWebController alloc] init];
        detailVC.dataModel = self.totalArr[indexPath.row];
        detailVC.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:detailVC animated:YES];
    } else if (column == 1) {
        SNKPhotoShowViewController *photoShow = [[SNKPhotoShowViewController alloc] init];
        photoShow.currentIndex = (int)indexPath.row;
        photoShow.mutaArray = self.totalArr;
        [self.navigationController pushViewController:photoShow animated:YES];
    } else {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        SNKVideoCollectModel *model = self.totalArr[indexPath.row];
        
        SNKVideoDetailController *detailVC = [[SNKVideoDetailController alloc] init];
        detailVC.videoCover  = model.image_url;
        detailVC.videoUrl = model.video_url;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

// 提交编辑模式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (column == 0) {
        SNKDataModel *dataModel = self.totalArr[indexPath.row];
        [SNKDataBase deleteNewsTableWithDocid:dataModel.docid];
        self.totalArr = [SNKDataBase newsDisplay];
    } else if (column == 1) {
        PhotoCollectModel *photoCollectModel = self.totalArr[indexPath.row];
        [SNKDataBase deletePhotosTableWithImage_url:photoCollectModel.image_url];
        self.totalArr = [SNKDataBase photosDisplay];
    } else {
        SNKVideoCollectModel *videoCollectModel = self.totalArr[indexPath.row];
        [SNKDataBase deleteVideosTableWithImage_url:videoCollectModel.image_url];
        self.totalArr = [SNKDataBase videosDisplay];
    }
    [tableView reloadData];
    [tableView setEditing:NO animated:YES];
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)dealloc
{
    [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"Segment"];
}

@end
